package com.shiliuzi.www.util.mqUtil;

import com.rabbitmq.client.Channel;
import com.shiliuzi.www.pojo.dto.download.DownloadExcelCtrlDto;
import com.shiliuzi.www.pojo.dto.user.UserLoginMQDto;
import com.shiliuzi.www.service.DownloadService;
import com.shiliuzi.www.util.CommonUtil;
import com.shiliuzi.www.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

import static com.shiliuzi.www.config.RabbitMQConfig.*;

@Slf4j
@Component
public class MQRead<T> {

    @Resource
    private CommonUtil commonUtil;

    @Resource
    private DownloadService downloadService;

    @Resource
    private EmailUtil emailUtil;

    /**
     * 拒绝消费
     * @param message
     * @param channel
     * @throws Exception
     */
//    @RabbitListener(queues = DEAD_QUEUE)
//    public void ListenerDeadTTLQueue(Message message, Channel channel) throws Exception{
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            String info = new String(message.getBody());
//            DownloadExcelCtrlDto downloadExcelCtrlDto =
//                    commonUtil.jsonReader(info, DownloadExcelCtrlDto.class);
//            System.out.println(downloadExcelCtrlDto);
//            channel.basicReject(deliveryTag,false);
//        } catch (Exception e) {
//            channel.basicNack(deliveryTag,true,true);
//        }
//    }

    /**
     * 延迟队列执行方法--保留时间之后即可触发
     * @param message
     * @param channel
     */
    @RabbitListener(queues = DEAD_LETTER_QUEUE)
    public void ListenerDeadQueue(Message message, Channel channel) throws Exception{
        String info = new String(message.getBody());
        DownloadExcelCtrlDto downloadExcelCtrlDto =
                commonUtil.jsonReader(info, DownloadExcelCtrlDto.class);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("现在处理消息{}！请耐心等待！",deliveryTag);
            downloadService.excelCtrlInfo(downloadExcelCtrlDto);
            channel.basicAck(deliveryTag,true);
            commonUtil.sleepThread(Thread.currentThread().getId(),20l);
        } catch (Exception e) {
            System.out.println("系统内部出错！延迟队列宕机！");
            channel.basicNack(deliveryTag,true,true);
        }
    }

    /**
     * 发送邮箱验证码消息队列
     * @param message
     * @param channel
     */
    @RabbitListener(queues = EMAIL_QUEUE)
    public void ListenerEmailQueue(Message message, Channel channel) throws Exception{
        String info = new String(message.getBody());
        UserLoginMQDto userLoginMQDto = commonUtil.jsonReader(info, UserLoginMQDto.class);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("现在处理消息{}！请耐心等待！",deliveryTag);
            emailUtil.sendMessage(userLoginMQDto.getEmail(),userLoginMQDto.getMessage(),userLoginMQDto.getCode());
            channel.basicAck(deliveryTag,true);
            commonUtil.sleepThread(Thread.currentThread().getId(),20l);
        } catch (Exception e) {
            System.out.println("系统内部出错！验证码发送失败！");
            commonUtil.sleepThread(Thread.currentThread().getId(),30l);
            channel.basicNack(deliveryTag,true,true);
        }
    }
}
